//数据交互管理类
var ServerMessageManager = (function () {
    var serverUrl = domain;//服务端交互地址
    // var dataLoading = new loading.DataLoading();
    /*保存分数*/
    function saveScore(data, successCallBack, errorCallBack) {
        $.ajax({
            // timeout: 4000, //超时时间设置，单位毫秒
            type: "POST",
            data: data,
            // url: serverUrl + "/index.php/saveScore",
            url: "./saveScore.json",
            // dataType: "json",//返回的数据格式
            success: function (result) { //请求成功的回调函数
                if (isFunction(successCallBack)) {
                    successCallBack(result);
                }
            },
            error: function (result) {
                if (isFunction(errorCallBack)) {
                    errorCallBack(result);
                }
            }
        });
    }

    /*获取分数列表*/
    function scoreList(data, successCallBack, errorCallBack) {
        $.ajax({
            // timeout: 4000, //超时时间设置，单位毫秒
            type: "POST",
            data: data,
            url: "./scoreList.json",
            // url: serverUrl + "/index.php/scoreList",
            // dataType: "json",//返回的数据格式
            success: function (result) { //请求成功的回调函数
                if (isFunction(successCallBack)) {
                    successCallBack(result);
                }
            },
            error: function (result) {
                if (isFunction(errorCallBack)) {
                    errorCallBack(result);
                }
            }
        });
    }

    /*获取相邻两边分数*/
    function scoreAdjacenList(data, successCallBack, errorCallBack) {
        $.ajax({
            // timeout: 4000, //超时时间设置，单位毫秒
            type: "POST",
            data: data,
            url: "./scoreAdjacenList.json",
            // url: serverUrl + "..//scoreAdjacenList",
            // dataType: "json",//返回的数据格式
            success: function (result) { //请求成功的回调函数
                if (isFunction(successCallBack)) {
                    successCallBack(result);
                }
            },
            error: function (result) {
                if (isFunction(errorCallBack)) {
                    errorCallBack(result);
                }
            }
        });
    }


    return {
        saveScore: saveScore,
        scoreList: scoreList,
        scoreAdjacenList: scoreAdjacenList

    };
})();


var rankingManager = (function () {
    function rankingOwn(s,page) {
        s.count_txt.text = userInfo.score;
        var data = {};
        data.openid = userInfo.openid;
        data.limit = 10;
        data.gameId = userInfo.gameId;
        ServerMessageManager.scoreAdjacenList(data, function (e) {
            var result = e;
            // var result = JSON.parse(e);
            if (result.status == 0) {
                s.shot = new annieUI.ScrollList(page, 210, 300, 630, 300, false);
                var length = result.data.list.length;
                s.shot.updateData(result.data.list);
                s.listCon.addChild(s.shot);
                if (result.data.position > 2) {
                    s.shot.scrollTo(210 * (result.data.position - 2));
                }
            }
        });
    }

    function rankingAll(s,page) {
        var data = {};
        data.order = "DESC";
        data.limit = 7;
        data.page = 1;
        data.gameId = userInfo.gameId;
        data.openid = userInfo.openid;
        ServerMessageManager.scoreList(data, function (e) {
            // var result = JSON.parse(e);
            var result = e;
            if (result.status == 0) {
                var shot = new annieUI.ScrollList(page, 630, 130, 630, 780);
                s.itemList_con.addChild(shot);
                shot.updateData(result.data.list, true);
            }
            var currentRaink = result.data.list[result.data.position - 1];
            s.cid_txt.text = result.data.position;
            s.score_txt.text = currentRaink.score;
            s.name.text = currentRaink.nickname;
            s.rectFace.init(currentRaink.headimgurl, 89, 1);
        });
    }

    function saveScore(count,successCallBack) {
        var data = {};
        data.openid = userInfo.openid;
        data.score = count;
        data.mode = "WRITE";
        data.gameId = userInfo.gameId;
        userInfo.score = count;
        ServerMessageManager.saveScore(data, function (e) {
            if (isFunction(successCallBack)) {
                successCallBack(e);
            }
        });
    }

    return {
        rankingOwn: rankingOwn,
        rankingAll: rankingAll,
        saveScore: saveScore
    }
})();


//判断身份证
function isCardNo(card) {
    // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X
    var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|\.|\.)$)/;
    if (reg.test(card) === false) {
        // alert("身份证输入不合法");
        return false;
    }
    return true;
}


function isPhoen(phone) {
    var reg = /^\d{11}$/;
    if (!reg.test(phone)) {
        // alert("请输入11位手机号码！")
        return false;
    }
    return true;
}

/*生成签名(华哥sha1)*/
function sha1Signature(paramsObj, secret) {
    var signature = "",
        md5SignatureStr = "";

    function objKeySort(obj) {//排序的函数
        var newkey = Object.keys(obj).sort();
        //先用Object内置类的keys方法获取要排序对象的属性名，再利用Array原型上的sort方法对获取的属性名进行排序，newkey是一个数组
        var newObj = {};//创建一个新的对象，用于存放排好序的键值对
        for (var i = 0; i < newkey.length; i++) {//遍历newkey数组
            newObj[newkey[i]] = obj[newkey[i]];//向新创建的对象中按照排好的顺序依次增加键值对
        }
        return newObj;//返回排好序的新对象
    }

    paramsObj.request_secret = secret;
    // console.log(paramsObj)

    // trace('钥匙：' + secret);
    var dataObj = objKeySort(paramsObj);//函数执行
    var arr = Object.keys(dataObj);
    var len = arr.length;
    var current = 0;
    // trace(dataObj);
    for (var key in dataObj) {
        if (current == len - 1) {
            signature += key + "=" + dataObj[key];
        } else {
            signature += key + "=" + dataObj[key] + "&";
        }
        current++;
    }
    // signature += "request_secret="+secret;
    // console.log(signature);
    // trace('signature:' + signature);
    md5SignatureStr = sha1(signature);
    trace('sha1Signature:' + md5SignatureStr);
    return md5SignatureStr;
}

/*生成签名*/
function md5Signature(paramsObj, secret) {
    var signature = "",
        md5SignatureStr = "";

    function objKeySort(obj) {//排序的函数
        var newkey = Object.keys(obj).sort();
        //先用Object内置类的keys方法获取要排序对象的属性名，再利用Array原型上的sort方法对获取的属性名进行排序，newkey是一个数组
        var newObj = {};//创建一个新的对象，用于存放排好序的键值对
        for (var i = 0; i < newkey.length; i++) {//遍历newkey数组
            newObj[newkey[i]] = obj[newkey[i]];//向新创建的对象中按照排好的顺序依次增加键值对
        }
        return newObj;//返回排好序的新对象
    }

    // trace('钥匙：' + secret);
    var dataObj = objKeySort(paramsObj);//函数执行
    // trace(JSON.stringify(dataObj));
    for (var key in dataObj) {
        signature += dataObj[key];
    }
    signature += secret;
    // trace('signature:' + signature);
    md5SignatureStr = md5(signature);
    // trace('md5Signature:' + md5SignatureStr);
    return md5SignatureStr;
}


//获取地址栏参数
function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

/*判断是否是函数*/
function isFunction(fn) {
    return Object.prototype.toString.call(fn) === '[object Function]';
}

/*获取当前时间并格式化*/
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    var hours = date.getHours();
    var minutes = date.getMinutes();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    if (hours >= 0 && hours <= 9) {
        hours = "0" + hours;
    }
    if (minutes >= 0 && minutes <= 9) {
        minutes = "0" + minutes;
    }
    var currentdate = date.getFullYear() + "年" + month + "月" + strDate
        + "日 " + hours + seperator2 + minutes
        + seperator2 + date.getSeconds();
    return currentdate;
};

//判断是否十位以内
function isTen(res) {
    var data = res.length;
    if (data > 10) {
        return false;
    }
    return true;
}